package com.sxm.yiwei.adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.sxm.yiwei.R;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;

/**
 * 卖家- 订单-待选定列表
 * Created by KuKa on 2017/6/9.
 */

public class YivierSelectAdapter extends BaseAdapter {
    private Context context;
    private List<HashMap<String,String>>list;
    private Callback mCallback;

    public interface  Callback{
        void onclick(int v);
        void onclick1(int v);
    }
    public void setOnClick(Callback callback){
        this.mCallback=callback;
    }

    public YivierSelectAdapter(Context context, List<HashMap<String,String>> list) {
        this.context=context;
        this.list=list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
       ViewHolder holder;
        View view;
        if (convertView==null){
            view = View.inflate(context, R.layout.select_yivier_item,null);
            holder = new ViewHolder();
            holder.choose = (Button) view.findViewById(R.id.yivier_choosebt);
            holder.cancel = (Button) view.findViewById(R.id.yivier_cancelbt);
            holder.distance = (TextView) view.findViewById(R.id.yivier_distance);
            holder.billno = (TextView) view.findViewById(R.id.yivier_billno);
            holder.yivier = (TextView) view.findViewById(R.id.text_yivier);
            view.setTag(holder);
        }else {
            view=convertView;
            holder= (ViewHolder) view.getTag();
        }

        String id = list.get(position).get("id");
        holder.yivier.setText(list.get(position).get("nickname"));
        holder.billno.setText(list.get(position).get("orderTakenCount"));
        String dd = list.get(position).get("distance");
        double v = Double.valueOf(dd) / 1000;
        DecimalFormat df = new DecimalFormat("#.00");
        holder.distance.setText(df.format(v)+" 公里");
        holder.choose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCallback!=null){
                    mCallback.onclick(position);
                }
            }
        });
        holder.cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCallback!=null){
                    mCallback.onclick1(position);
                }
            }
        });
        notifyDataSetChanged();

        return view;
    }

    class ViewHolder{
        private TextView yivier,billno,distance;
        private Button cancel,choose;
    }

}
